/*
 * Copyright (c) 2006-2024, RT-Thread Development Team
 *
 * SPDX-License-Identifier: Apache-2.0
 *
 * Change Logs:
 * Date           Author       Notes
 * 2024-08-11     RT-Thread    first version
 */
#include <rtthread.h>
#include <rtdevice.h>
#include <dfs_file.h>

#include "drv_qspi.h"
#include "drv_gpio.h"
#include "spi_flash_sfud.h"

#define QSPI_BUS_NAME       "qspi1"
#define QSPI_DEVICE_NAME    "qspi10"
#define EN25Q_FLASH_NAME    "flash0"
#define QSPI_CS_PIN         GET_PIN(G, 6)


static rt_spi_flash_device_t spi_flash_dev;


static int filefs_init(void)
{
    int ret;

    LOG_I("FileFS Init!");

    ret = at32_qspi_bus_attach_device(QSPI_BUS_NAME, QSPI_DEVICE_NAME, QSPI_CS_PIN, 4, NULL, NULL);
    if (ret != RT_EOK)
    {
        LOG_E("Can't attach QSPI, ret %d", ret);
        return ret;
    }

#ifdef  RT_USING_SFUD
    if ((spi_flash_dev = rt_sfud_flash_probe(EN25Q_FLASH_NAME, QSPI_DEVICE_NAME)) == RT_NULL)
    {
        LOG_E("Probe QSPI flash failed");
        return -RT_ERROR;
    }
#endif

    ret = dfs_mount(EN25Q_FLASH_NAME, "/", "elm", 0, 0);
    if (ret != RT_EOK)
    {
        LOG_E("DFS mount failed, ret %d", ret);
        return -RT_ERROR;
    }

    return RT_EOK;
}

INIT_ENV_EXPORT(filefs_init);


